package com.conan.dailyflickr.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PreferenceUtil {
	
	/**
	 * get specific string from SharedPreferences
	 * @param The context of the preferences whose values are wanted.
	 * @param preferenceName preference name
	 * @param key The name of the preference to retrieve.
	 * @param defValue default value.
	 * @return Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String
	 */
	public static String getStringFromSharedPreferences(
			Context context, String preferenceName, String key, String defValue)
			throws ClassCastException, NullPointerException {
		SharedPreferences prefs = context.getSharedPreferences(
				preferenceName, /* MODE_PRIVATE */0);
		return prefs.getString(key, defValue);
	}
	
	/**
	 * Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.
	 * @param context The context of the preferences whose values are wanted.
	 * @param preferenceName preference name
	 * @param key The name of the preference to retrieve.
	 * @param defValue default value.
	 * @return Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String
	 */
	public static String getStringFromDefaultSharedPreferences(
			Context context, String key, String defValue)
			throws ClassCastException, NullPointerException {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		return prefs.getString(key, defValue);
	}
	
	/**
	 * Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.
	 * @param context The context of the preferences whose values are wanted.
	 * @param preferenceName preference name
	 * @param key The name of the preference to retrieve.
	 * @param defValue default value.
	 * @return Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String
	 */
	public static boolean getBooleanFromDefaultSharedPreferences(
			Context context, String key, boolean defValue)
			throws ClassCastException, NullPointerException {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		return prefs.getBoolean(key, defValue);
	}
	
	/**
	 * Save specific string to SharedPreferences
	 * @param context The context of the preferences whose values are wanted.
	 * @param name Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit())
	 * @param key The name of the preference to modify.
	 * @param value The new value for the preference.
	 */
	public static void saveSharedPreferences(Context context, String name, String key,
			String value) {
		SharedPreferences pref = context.getSharedPreferences(
				name, /* MODE_PRIVATE */0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	/**
	 * Save specific string to default SharedPreferences
	 * @param context The context of the preferences whose values are wanted.
	 * @param key The name of the preference to modify.
	 * @param value The new value for the preference.
	 */
	public static void saveDefaultPreferences(Context context, String key,
			String value) {
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	/**
	 * Save specific string to default SharedPreferences
	 * @param context The context of the preferences whose values are wanted.
	 * @param key The name of the preference to modify.
	 * @param value The new value for the preference.
	 */
	public static void saveDefaultPreferences(Context context, String key,
			boolean value) {
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}
	
//	public static void savePref(Context context, String name, String key,
//			int value) {
//		SharedPreferences settings = context.getSharedPreferences(
//				name, /* MODE_PRIVATE */0);
//		SharedPreferences.Editor editor = settings.edit();
//		
//		editor.putInt(key, value);
//
//		editor.commit();
//	}
	
//	public static int getPref(Context context, String constants, String key, int defValue) {
//		SharedPreferences settings = context.getSharedPreferences(constants, /* MODE_PRIVATE */0);
//		return settings.getInt(key, defValue);
//	}
//	
//	public static String getPref(Context context, String constants, String key, String defValue) {
//		String result = null;
//		SharedPreferences settings = context.getSharedPreferences(constants, /* MODE_PRIVATE */0);
//		result = settings.getString(key, defValue);
//		return result;
//	}
	
}
